package com.lumlord.service;

import java.util.List;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lumlord.model.custom.Page;
import com.lumlord.common.exception.BusinessException;
import com.lumlord.common.base.ApiService;
import com.lumlord.mapper.auto.AutoCode;
import com.lumlord.mapper.auto.AutoCodeExample;
import com.lumlord.mapper.auto.AutoCodeMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;

/**
 * 自动代码生成 AutoCodeService
 * @Title: AutoCodeService.java 
 * @Package com.lumlord.service 
 * @author gpf_自动生成
 * @email 372138617@qq.com
 * @date 2019-09-23 12:45:51  
 **/
@Service
public class AutoCodeService implements ApiService<AutoCode, AutoCodeExample>{
	
      	   	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<AutoCode> list(Page page) throws BusinessException{
	        AutoCodeExample testExample=new AutoCodeExample();
	        testExample.setOrderByClause("id DESC");
	        PageHelper.startPage(page.getPageNum(), page.getPageSize());
	        List<AutoCode> list= autoCodeMapper.selectByExample(testExample);
	        PageInfo<AutoCode> pageInfo = new PageInfo<AutoCode>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(Integer[] ids) throws BusinessException{
		AutoCodeExample example=new AutoCodeExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		return autoCodeMapper.deleteByExample(example);
	}
	
	
	
	@Override
	public AutoCode selectByPrimaryKey(Integer id) throws BusinessException{
		
		return autoCodeMapper.selectByPrimaryKey(id);
	}

	
	@Override
	public int updateByPrimaryKeySelective(AutoCode record) throws BusinessException{
		return autoCodeMapper.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(AutoCode record) throws BusinessException{
		return autoCodeMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(AutoCode record, AutoCodeExample example) throws BusinessException{
		
		return autoCodeMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(AutoCode record, AutoCodeExample example) throws BusinessException{
		
		return autoCodeMapper.updateByExample(record, example);
	}

	@Override
	public List<AutoCode> selectByExample(AutoCodeExample example) throws BusinessException{
		
		return autoCodeMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(AutoCodeExample example) throws BusinessException{
		
		return autoCodeMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(AutoCodeExample example) throws BusinessException{
		
		return autoCodeMapper.deleteByExample(example);
	}
	
	 /**
     * 逻辑删除
     * @param ids
     * @return
     */
	@Override
	public int del(Integer[] ids) throws BusinessException{
		return autoCodeMapper.del(ids);
	}
	
	 /**
          *批量插入
     * @param record
     * @return
     */
	@Override
	public int inserBatch(List<AutoCode> records) throws BusinessException{
		SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);//跟上述sql
		int count = 0;
		for(AutoCode record : records) {
			count+=autoCodeMapper.insert(record);
		}
		sqlSession.commit();
		return count;
	}
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Autowired
	private AutoCodeMapper autoCodeMapper;
	
}
